package com.dp;

public class Solution07 {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if (obstacleGrid[0][0] == 1) return 0;
        int flag = 1;
        for (int i = 0; i < obstacleGrid.length; i++) {
            if (flag==0) obstacleGrid[i][0] = 0;
            else if (obstacleGrid[i][0] == 1) {
                flag =0;
                obstacleGrid[i][0] = 0;
            }else obstacleGrid[i][0] = 1;
        }
        flag=1;
        for (int i = 1; i < obstacleGrid[0].length; i++) {
            if (flag==0) obstacleGrid[0][i] = 0;
            else if (obstacleGrid[0][i] == 1) {
                flag =0;
                obstacleGrid[0][i] = 0;
            }else obstacleGrid[0][i] = 1;
        }
        for (int i = 1; i < obstacleGrid.length; i++) {
            for (int j = 1; j < obstacleGrid[0].length; j++) {
                obstacleGrid[i][j] = obstacleGrid[i][j] > 0 ? 0 : obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
            }
        }
        return obstacleGrid[obstacleGrid.length-1][obstacleGrid[0].length-1];
    }
}
